import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

//下方弹出popupWindow选择框
//使用showCupertinoModalPopup+CupertinoPicker实现效果

class CommonPicker {
  static Future<int> showPicker({
    BuildContext context,
    List<String> options,
    int value,
    double height = 300
  }) {
    //相当于android中的popupWindow
    return showCupertinoModalPopup<int>(
        context: context,
        builder: (BuildContext context) {
          var buttonTextStyle = TextStyle(
            color: Theme.of(context).primaryColor,
            fontWeight: FontWeight.w600,
          );
          //选择器controller
          var controller = FixedExtentScrollController(initialItem: value);
          return Container(
            color: Colors.grey,
            height: height,
            child: Column(
              children: [
                Container(
                  color: Colors.grey[200],
                  height: 40,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      TextButton(onPressed: () {
                        Navigator.of(context).pop();//取消就不传值
                      }, child: Text('取消',style: buttonTextStyle,)),
                      TextButton(onPressed: () {
                        //通过pop把值返回
                        Navigator.of(context).pop(controller.selectedItem);
                      }, child: Text('确定',style: buttonTextStyle)),
                    ],
                  ),
                ),
                Expanded(child: CupertinoPicker(//滚动选择列表
                  scrollController: controller,
                  backgroundColor: Theme.of(context).scaffoldBackgroundColor,
                  itemExtent: 32.0,//item高度
                  //item选择事件
                  onSelectedItemChanged: (val){

                  },
                  //item列表
                  children: options.map((item) => Text(
                    item
                  )).toList(),
                ))
              ],
            )

          );
        });
  }
}
